import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import freemind.controller.NodeAdapterRiscos;
import freemind.controller.TheMap;


public class Teste {

	public static void main(String[] args) {

		NodeAdapterRiscos node1 = new NodeAdapterRiscos();
		
		node1.setNome("Raiz");
		node1.setAutor("Daniel");
		node1.setId(1);
		node1.setIdParent(0);
		node1.setIdRaiz(1);
		node1.setTexto("RAIZ TESTE");
		node1.setVirtualId(1);
		
		NodeAdapterRiscos node2 = new NodeAdapterRiscos();
		
		node2.setNome("Filho");
		node2.setAutor("Daniel");
		node2.setId(2);
		node2.setIdParent(1);
		node2.setIdRaiz(1);
		node2.setTexto("Filho 1");
		node2.setVirtualId(2);
		
		NodeAdapterRiscos node3 = new NodeAdapterRiscos();
		
		node3.setNome("Filho 2");
		node3.setAutor("Daniel");
		node3.setId(3);
		node3.setIdParent(1);
		node3.setIdRaiz(1);
		node3.setTexto("Filho 3");
		node3.setVirtualId(3);		
		
		node1.addChild(node2);
		node1.addChild(node3);
				
		TheMap map = new TheMap(node1);
		map.addCriados(node1);
		map.setProtocolo(TheMap.RESPONSE_LOAD);
		
		byte[] teste = writeMap(map);
		
		TheMap map2 = (TheMap) readMap(teste);
		
		System.out.println(map2.getProtocolo());
		System.out.println(map2.getRoot().getId());
		System.out.println(map2.getRoot().getTexto());
		System.out.println(map2.getCriados().size());
		
		System.out.println(map2.getRoot().getChildren().size());
		System.out.println(map2.getRoot().getChildren().get(0).getTexto());
	}

	private static Object readMap(byte[] s){
		
		try {
			ByteArrayInputStream bais = new ByteArrayInputStream( s );
			
//			ObjectInputStream ois = new ObjectInputStream(new FileInputStream("C:/Daniel/teste.xml"));
			ObjectInputStream ois = new ObjectInputStream(bais);
			
			TheMap map = (TheMap) ois.readObject();
			ois.close();
			
			return map;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	private static byte[] writeMap(Object map){
		
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			
//			ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream("C:/Daniel/teste.xml") );						
			
			ObjectOutputStream oos = new ObjectOutputStream( baos );
			
			oos.writeObject(map);
			
			oos.flush();
			oos.close();

			return baos.toByteArray();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
							
		return new byte[10];
	}	
	
}
